package com.example.controller;

import com.example.common.Result;
import com.example.entity.Admin;
import com.example.service.AdminService;
import com.github.pagehelper.PageInfo;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * 管理员信息表(Admin)表控制层
 *
 */
@CrossOrigin//解决跨域问题注解
@RestController
//@RequestMapping("/admin")
public class AdminController {
    @Resource
    private AdminService adminService;

    //用户登录
    @ResponseBody
    @RequestMapping("adminLogin")
    public Result Login(Admin user,HttpServletRequest request,HashMap map) {
        Admin admin = adminService.login(user, request, map);
        return Result.success(admin);
    }

    //用户注册
    @ResponseBody
    @RequestMapping("adminReg")
    public Result adminReg(Admin admin ,HttpServletRequest request,HashMap map) {
        adminService.register(admin, request, map);
        return Result.success();
    }

    //信息列表
    @ResponseBody
    @RequestMapping("adminList")
    public Result  adminList(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum, @RequestParam(defaultValue = "1",value = "pageSize") Integer pageSize, HttpServletRequest request){
        String key = request.getParameter("key");
        String key1 = request.getParameter("key1");
        String key2 = request.getParameter("key2");
        String role = request.getParameter("role");
        PageInfo<Admin> pageInfo = adminService.getAdminList(pageNum, pageSize, key, key1, key2, role);
        return Result.success(pageInfo);
    }

    //添加或编辑信息
    @ResponseBody
    @RequestMapping("adminAdd")
    public Result adminAdd(Admin admin ,HttpServletRequest request) {
        adminService.saveOrUpdate(admin, request);
        return Result.success();
    }

    //修改密码
    @ResponseBody
    @RequestMapping("adminpassEdit")
    public Result adminpassEdit(Admin admin ,HttpServletRequest request) {
        String onlpassword = request.getParameter("onlpassword");
        String confirmPasword = request.getParameter("confirmPasword");
        adminService.updatePassword(admin, onlpassword, confirmPasword);
        return Result.success();
    }

    //删除信息
    @ResponseBody
    @RequestMapping("adminDel")
    public Result adminDel(int id,HttpServletRequest request) {
        adminService.deleteAdmin(id);
        return Result.success();
    }

    //批量删除信息
    @ResponseBody
    @RequestMapping("adminDeleteAll")
    public Result typeDeleteAll(HttpServletRequest request) {
        String vals = request.getParameter("vals");
        adminService.batchDeleteAdmin(vals);
        return Result.success();
    }

}

